package com.ruoyi.system.service.impl;



import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ruoyi.common.core.exception.GlobalException;
import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.ruoyi.system.domain.Test;
import com.ruoyi.system.domain.Test2;
import com.ruoyi.system.domain.dto.CreditCaseCriteria;
import com.ruoyi.system.domain.dto.TestDto;
import com.ruoyi.system.domain.vo.TestVo;
import com.ruoyi.system.service.TestService;
import com.ruoyi.system.mapper.TestMapper;

import org.springframework.stereotype.Service;

import java.util.Date;


/**
*
*/
@Service
public class TestServiceImpl extends ServiceImpl<TestMapper, Test> implements TestService{


    @Override
    public Page<TestVo> getTestByPage(CreditCaseCriteria pageEntity) {

        MPJLambdaWrapper<Test> wrapper = new MPJLambdaWrapper<Test>()
                .selectAll(Test.class)//查询user表全部字段
                .select(Test2::getAddress, Test2::getEmail)
                .leftJoin(Test2.class, Test2::getTestId, Test::getId)
                //记的测试阶段
                .eq(Test::getDelFlag, 0)
                .orderByDesc(Test::getCreateTime);
        Page<TestVo> userList = getBaseMapper().selectJoinPage(new Page<>(1, 10),   TestVo.class, wrapper);

        return userList;
    }

    @Override
    public void addTest(TestDto testDto) {
        MPJLambdaWrapper<Test> wrapper = new MPJLambdaWrapper<Test>()
              .eq(Test::getName, testDto.getName());

        Test test = getBaseMapper().selectOne(wrapper);
        if(null != test) {
            throw new GlobalException("该Test已经存在");
        }
        Test test2 = new Test();
        BeanUtils.copyProperties(testDto, test2, new String[]{"id"});
        getBaseMapper().insert(test2);
    }

    @Override
    public void updateTest(TestDto testDto) {
        MPJLambdaWrapper<Test> wrapper = new MPJLambdaWrapper<Test>()
                .eq(Test::getName, testDto.getName());

        Test test = getBaseMapper().selectOne(wrapper);
        if(null == test) {
            throw new GlobalException("该Test不存在");
        }
        BeanUtils.copyProperties(testDto, test);
        test.setCreateBy("admin");
        test.setCreateTime(new Date());
        getBaseMapper().updateById(test);
    }

    @Override
    public void delTest(TestDto testDto) {
        Test test = getBaseMapper().selectById(testDto.getId());
        if(null == test) {
            throw new GlobalException("该Test不存在");
        }
        test.setDelFlag(true);
        getBaseMapper().updateById(test);
    }
}
